using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Horizon;

namespace HorizonTestClient
{
    public partial class LibraryForm : Form
    {
        private Library _lib;

        public LibraryForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Library object being managed in this dialog
        /// </summary>
        public Library CurrentLibrary
        {
            get { return _lib; }
            set { _lib = value; }
        }

        /// <summary>
        /// Updates the CurrentLibrary object with the entered details.
        /// Also dismisses the form since button is DialogResult.OK
        /// </summary>
        private void updateLibraryButton_Click(object sender, EventArgs e)
        {
            _lib.Name = libraryNameTextBox.Text;
            _lib.BaseUrl = baseURLTextBox.Text;
            _lib.Profile = profileTextBox.Text; 

            try
            {
                // Try connecting to the library.  If all is good, return
                _lib.Init();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch( Exception ex)
            {
                MessageBox.Show("Error reading library information: " + ex.Message);
            }
        }

        /// <summary>
        /// Preloads the display fields with the CurrentLibrary when form is shown
        /// </summary>
        private void LibraryForm_Shown(object sender, EventArgs e)
        {
            if (_lib != null)
            {
                libraryNameTextBox.Text = _lib.Name;
                baseURLTextBox.Text = _lib.BaseUrl;
                profileTextBox.Text = _lib.Profile;
            }

            libraryNameTextBox.Focus();
        }

        /// <summary>
        /// Verifies that URL appears valid, otherwise displays a tooltip.
        /// Does not prevent bad data, just warns...
        /// </summary>
        private void baseURLTextBox_Leave(object sender, EventArgs e)
        {
            string baseUrl = baseURLTextBox.Text;
            
            toolTip1.Hide(baseURLTextBox);

            // Ignore if field is empty
            if (string.IsNullOrEmpty(baseUrl)) return;

            // Make sure that URL is well-formed
            if (!Uri.IsWellFormedUriString(baseUrl, UriKind.Absolute))
            {
                toolTip1.Show("Tip: URL does not appear to be valid", baseURLTextBox, 3000);
            }

            // Horizon Information Portal URL's all seem to end with /ipac20/ipac.jsp
            // Of course that could change someday...
            if (!baseUrl.EndsWith("ipac.jsp"))
            {
                toolTip1.Show("Tip: URL's generally end with /ipac20/ipac.jsp", baseURLTextBox, 3000);
            }
        }
    }
}